/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model.DAO;

import Model.Beans.Book;
import Model.Beans.Tag;
import Model.Beans.User;
import Model.Utilities.Constantes.BookState;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author sixviv
 */
public class BookDAO extends AbstracDAO<Book>{

    public List<Book> getBooksInstitution(User userInstitution){
        List<Book> books=null;
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria bookCri = session.createCriteria(Book.class);
        bookCri.add(Restrictions.eq("status", BookState.APROBADO));
        books = bookCri.list();
        session.close();
        return books;
    }
    
    public void insertBookTags(Book book){
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            transaccion = session.beginTransaction();
            session.save(book);
            transaccion.commit();
            session.close();
            
            ArrayList<Tag> tags = new ArrayList<Tag>(book.getTags());
            for(int i = 0; i < tags.size(); i++){
                tags.get(i).setBook(book);
                session = HibernateUtil.getSessionFactory().openSession();
                transaccion = session.beginTransaction();
                session.clear();
                session.save(tags.get(i));
                transaccion.commit();
                session.close();
            }
            
        }catch(Exception e){
            System.out.println(e.getMessage());
            transaccion.rollback();
            session.close();
        }
    }

    public List<Book> getBooksInstitutionSol(User userInstitution){
        List<Book> books=null;
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria bookCri = session.createCriteria(Book.class);
        bookCri.setFetchMode("user", FetchMode.JOIN);
        bookCri.add(Restrictions.eq("user.iduser", userInstitution.getIduser()));
        bookCri.add(Restrictions.eq("status", BookState.SOLICITADO));
        books = bookCri.list();
        session.close();
        return books;
    }

    public List<Book> getBooksInstitutionPen(User userInstitution){
        List<Book> books=null;
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria bookCri = session.createCriteria(Book.class);
        bookCri.setFetchMode("user", FetchMode.JOIN);
        bookCri.add(Restrictions.eq("user.iduser", userInstitution.getIduser()));
        bookCri.add(Restrictions.eq("status", BookState.PENDIENTE));
        books = bookCri.list();
        session.close();
        return books;
    }
    
    

    public Book getBook(int idBook){
        List<Book> books = buscar("idbook="+idBook, Book.class);
        if(books!=null&&!books.isEmpty()){
            return books.get(0);
        }else{
            return null;
        }
    }

    public List<Book> searchBooks(String[] tags, String nombre, String autor, String isbn, String editorial, String year, int idInstitucion){
        List<Tag> tagsList= new ArrayList<Tag>();
        List<Book> booksTemp = new ArrayList<Book>();
        List<Book> bookResult = new ArrayList<Book>();
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria bookCri = session.createCriteria(Book.class);

        if(!(nombre.compareTo("") == 0)) bookCri.add(Restrictions.eq("name", nombre));
        if(!(autor.compareTo("") == 0)) bookCri.add(Restrictions.eq("author", autor));
        if(!(isbn.compareTo("") == 0)) bookCri.add(Restrictions.eq("isbn", isbn));
        if(!(editorial.compareTo("") == 0)) bookCri.add(Restrictions.eq("editorial", editorial));
        if(!(year.compareTo("") == 0)) bookCri.add(Restrictions.eq("year", year));
        bookCri.add(Restrictions.eq("user.iduser", idInstitucion));
        bookCri.add(Restrictions.eq("status", BookState.APROBADO));   
        
        bookCri.setFetchMode("tags", FetchMode.JOIN);
        
        booksTemp = (List<Book>)bookCri.list();
        
        if(tags.length != 0){
            String condition = "WHERE this.tag = '";
            
            for(int i = 0; i < tags.length; i++){
                
                if(i == tags.length - 1) condition += tags[i] + "'";
                else condition += tags[i] + "' OR this.tag = '";
            }
            System.out.println(condition);
            for(Book book: booksTemp){
                tagsList = (List<Tag>) session.createFilter(book.getTags(), condition).list();
                System.out.println(tagsList.size());
                for(Tag tag: tagsList){
                    bookCri.add(Restrictions.eq("idbook", tag.getBook().getIdbook()));
                }
            }            
          }

        if(!tagsList.isEmpty()) bookResult = (List<Book>)bookCri.list();
        session.close();
        return bookResult;
    }
}
